package com.example.huangliting.experimentalarrangementsystem.adapter;

import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.util.TypedValue;
import android.view.Display;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.huangliting.experimentalarrangementsystem.R;
import com.example.huangliting.experimentalarrangementsystem.util.DateUitls;
import com.example.huangliting.experimentalarrangementsystem.view.KBCalendar;
import com.example.huangliting.experimentalarrangementsystem.view.TwoWayView;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class KBCalendarAdapter extends BaseAdapter {
    private final Context mContext;
    private ArrayList<Date> mListDays;
    private int widthScreen;
    public int widthCell;
    private KBCalendar kbCalendar;

    private int positionOfCenterItem;

    private int numberOfRowOnScreen;

    public KBCalendarAdapter(KBCalendar kbCalendar, Context context, ArrayList<Date> listDays, int numberOfRow) {
        mContext = context;
        mListDays = listDays;
        Display display = ((Activity) context).getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        widthScreen = size.x;
        numberOfRowOnScreen = numberOfRow;
        widthCell = widthScreen / numberOfRow;
        this.kbCalendar = kbCalendar;
    }

    @Override
    public int getCount() {
        return mListDays.size();
    }

    @Override
    public Integer getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public static class ViewHolderDay {
        public TextView txtDayNumber;
        public TextView txtWeekName;
        public TextView txtMonthName;
        public RelativeLayout layoutBackground;
        public View indicatorView;
        public View leftShadowView;
        public View rightShadowView;
        public View rightBorderView;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolderDay holder;

        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.list_item, parent, false);
            convertView.setLayoutParams(new TwoWayView.LayoutParams(widthCell, widthCell));
            holder = new ViewHolderDay();
            holder.txtDayNumber = (TextView) convertView.findViewById(R.id.dayNumber);
            holder.txtWeekName = (TextView) convertView.findViewById(R.id.weekName);
            holder.txtMonthName = (TextView) convertView.findViewById(R.id.monthName);
            holder.layoutBackground = (RelativeLayout) convertView.findViewById(R.id.layoutBackground);
            holder.indicatorView = (View) convertView.findViewById(R.id.indicator_view);
            holder.leftShadowView = (View) convertView.findViewById(R.id.left_shadow_view);
            holder.rightShadowView = (View) convertView.findViewById(R.id.right_shadow_view);
            holder.rightBorderView = (View) convertView.findViewById(R.id.right_border_view);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolderDay) convertView.getTag();
        }
        Date day = mListDays.get(position);
        if (day.after(kbCalendar.getToday())) {
            holder.txtWeekName.setTextColor(kbCalendar.outBoundColor);
            holder.txtDayNumber.setTextColor(kbCalendar.outBoundColor);
            holder.txtMonthName.setTextColor(kbCalendar.outBoundColor);
        } else if (kbCalendar.isDatesDaysEquals(day, kbCalendar.getToday())) {
            holder.txtWeekName.setTextColor(kbCalendar.selectColor);
            holder.txtMonthName.setTextColor(kbCalendar.selectColor);
            holder.txtDayNumber.setTextColor(kbCalendar.selectColor);
        } else {
            holder.txtWeekName.setTextColor(kbCalendar.normalColor);
            holder.txtMonthName.setTextColor(kbCalendar.normalColor);
            holder.txtDayNumber.setTextColor(kbCalendar.normalColor);
        }

        holder.txtWeekName.setTextSize(TypedValue.COMPLEX_UNIT_DIP, kbCalendar.getWeekSize() - 3);
        holder.txtDayNumber.setTextSize(TypedValue.COMPLEX_UNIT_DIP, kbCalendar.getDayNumberSize() - 3);
        holder.txtMonthName.setTextSize(TypedValue.COMPLEX_UNIT_DIP, kbCalendar.getMonthSize() - 3);

        setBackgroundColorToView(holder, position);
        Calendar cal = Calendar.getInstance();
        cal.setTime(day);
        holder.txtDayNumber.setText(String.format("%td", cal));
        holder.txtWeekName.setText(DateUitls.formateDay(cal.get(Calendar.DAY_OF_WEEK)));//String.format("%ta", cal)
        holder.txtMonthName.setText(cal.get(Calendar.MONTH) + 1 +"月");

        if (day.before(kbCalendar.getDateStartCalendar()) || day.after(kbCalendar.getDateEndCalendar())) {
            convertView.setVisibility(View.GONE);
        } else {
            convertView.setVisibility(View.VISIBLE);
        }
        if (position == getCount() - numberOfRowOnScreen / 2 - 1) {
            holder.rightBorderView.setVisibility(View.VISIBLE);
        } else {
            holder.rightBorderView.setVisibility(View.GONE);
        }
        return convertView;
    }

    private void setBackgroundColorToView(ViewHolderDay holder, int position) {
        boolean isMiddleView = isMiddleView(holder.layoutBackground, position);

        if (isMiddleView) {
            holder.txtWeekName.setTextSize(TypedValue.COMPLEX_UNIT_DIP, kbCalendar.getWeekSize());
            holder.txtDayNumber.setTextSize(TypedValue.COMPLEX_UNIT_DIP, kbCalendar.getDayNumberSize());
            holder.txtMonthName.setTextSize(TypedValue.COMPLEX_UNIT_DIP, kbCalendar.getMonthSize());
            holder.indicatorView.setBackgroundColor(kbCalendar.selectColor);
            holder.indicatorView.setVisibility(View.VISIBLE);
        } else {
            holder.indicatorView.setVisibility(View.GONE);
        }

        if (isLeftView(holder.layoutBackground)) {
            holder.rightShadowView.setVisibility(View.VISIBLE);
            holder.leftShadowView.setVisibility(View.GONE);
        } else if (isRightView(holder.layoutBackground)) {
            holder.rightShadowView.setVisibility(View.GONE);
            holder.leftShadowView.setVisibility(View.VISIBLE);
        } else {
            holder.rightShadowView.setVisibility(View.GONE);
            holder.leftShadowView.setVisibility(View.GONE);
        }
    }

    private boolean isMiddleView(View v, int position) {
        int[] loc = new int[2];
        v.getLocationOnScreen(loc);

        if (loc[0] < widthScreen / 2 && (loc[0] + widthCell) > widthScreen / 2) {
            positionOfCenterItem = position;
            return true;
        }
        return false;
    }

    private boolean isLeftView(View v) {
        int[] loc = new int[2];
        v.getLocationOnScreen(loc);

        if ((loc[0] + widthCell) < widthScreen / 2 && (loc[0] + widthCell * 2) > widthScreen / 2) {
            return true;
        }
        return false;
    }

    private boolean isRightView(View v) {
        int[] loc = new int[2];
        v.getLocationOnScreen(loc);

        if ((loc[0] - widthCell) < widthScreen / 2 && loc[0] > widthScreen / 2) {
            return true;
        }
        return false;
    }

    public int getPositionOfCenterItem() {
        return positionOfCenterItem;
    }
}
